<?php
	class Tk_accounts_subject extends MY_controller{

		function __construct(){
			parent::__construct();
			$this->load->model('Tk_accounts_subject_model');
		}

		function index(){
			$subject = $this->Tk_accounts_subject_model->get_top_subject_list();
			if($subject){
				foreach ($subject as $key => $value) {
					$subject[$key]['children'] = $this->get_subject_children($value['id']);
				}
			}
			$this->smarty->assign("subject", $subject);
			$this->smarty->display("tk_accounts_subject/list.html");
		}

		function get_subject_children($id){
			$children = $this->Tk_accounts_subject_model->get_subject_list_by_pid($id);
			return $children;
		}

		function ajax_get_subject_children(){
			$post_data = $this->input->post();
			if(is_ajax_request() && !empty($post_data['pid'])){
				$children = $this->get_subject_children($post_data['pid']);
				// echo $this->db->last_query();
				if($children) $code = 1;
				else $code = 0;
				echo json_encode(array("code" => $code, "children" => $children));
			}
		}
	}